﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.Model;

namespace MSBTC.WinForms
{
    public partial class MaterialsListForm : Form
    {
        PMSContext dbcontext = new PMSContext();
        MDIParent mdipar = new MDIParent();


        public MaterialsListForm()
        {
            InitializeComponent();
        }

        private void MaterialsListForm_Load(object sender, EventArgs e)
        {
            RefreshDB();
        }

        private void RefreshDB()
        {
            //retrieve all data
            var mtrlinv = (from m in dbcontext.MaterialsInventorys
                           select m).ToList();

            LVMatList.Items.Clear();
            if (mtrlinv.Count != 0)
            {
                foreach (var sqlMtrl in mtrlinv)
                {
                    //add items in listview box
                    ListViewItem lvitems = LVMatList.Items.Add(sqlMtrl.MaterialsInventoryid_PK.ToString());
                    lvitems.SubItems.Add(sqlMtrl.Material_Name.ToString());
                    lvitems.SubItems.Add(sqlMtrl.Qty.ToString());
                    lvitems.SubItems.Add(sqlMtrl.Unit.ToString());
                    lvitems.SubItems.Add(sqlMtrl.QtyUsed.ToString());
                    lvitems.SubItems.Add(sqlMtrl.UnitCost.ToString());
                }
            }
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            var mat = (from m in dbcontext.MaterialsInventorys
                       where m.Material_Name.Contains(txtSearch.Text)
                       select m).ToList();

            LVMatList.Items.Clear();
            if (mat != null)
            {

                foreach (var sqlMat in mat)
                {
                    ListViewItem lvitems = LVMatList.Items.Add(sqlMat.MaterialsInventoryid_PK.ToString());
                    lvitems.SubItems.Add(sqlMat.Material_Name.ToString());
                    lvitems.SubItems.Add(sqlMat.Brand_Name.ToString());
                    lvitems.SubItems.Add(sqlMat.Qty.ToString());
                    lvitems.SubItems.Add(sqlMat.Unit.ToString());
                    lvitems.SubItems.Add(sqlMat.QtyUsed.ToString());
                    lvitems.SubItems.Add(sqlMat.UnitCost.ToString());

                }

                if (txtSearch.Text == "")
                {
                    RefreshDB();
                }
            }
        }
        

    }
}
